package org.hagopa.games.moneypool.testClient;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Random;

import org.hagopa.games.moneypool.msgs.CommandsEnum;
import org.hagopa.games.moneypool.msgs.Message;
import org.hagopa.games.moneypool.msgs.ContentTypes.ChatMsg;
import org.hagopa.games.moneypool.msgs.ContentTypes.service.PlayerType;
import org.hagopa.games.moneypool.msgs.ContentTypes.service.Version;

public class TestClient implements Runnable{

	int port = 1234;
	private int clientId = 1;
	private ClientCLI ccli = null;
	public TestClient(int port)
	{
		this.port = port;
		System.out.println("Running Client");
		new Thread(this).start();
	}
	public void setCliId()
	{
		Random randGen = new Random();
		this.clientId = randGen.nextInt(200);
		System.out.println("Client Id:" + this.clientId);
	}
	public void setCli(ClientCLI ocli)
	{
		this.ccli = ocli;
	}
	public void run() 
	{
		System.out.println("Running Client in new Thread");
		setCliId();
		// TODO Auto-generated method stub
		Socket cs = null;
		BufferedInputStream in = null;
		BufferedOutputStream out = null;
		try {
			//cs = new Socket("127.0.0.1", port);
			cs = new Socket("bogo.com", port);
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		if(cs.isConnected())
		{
			try {
				in = new BufferedInputStream(cs.getInputStream());
				out = new BufferedOutputStream(cs.getOutputStream());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
				
		while(true)
		{		
			
			byte [] dst = new byte [1024];
			Message fromServer = null;
			try {
				if(cs.isConnected() == false)
				{
					System.out.println("socket disconnected");
					return;
				}
				 
				int bytes = in.read(dst);
				System.out.println("Received " + bytes + "bytes");
				fromServer = new Message();
				fromServer.unmarshall(dst);
					//(Message) ObjectConverter.getObj(dst);
				System.out.println("Received From CID: " + fromServer.getClientId());
				System.out.println("Received Commnad: " + fromServer.getCommand().getName());
				System.out.println("Received SessionId: " + fromServer.getSessionId());
				System.out.println("\n--------------------------------------------");
				System.out.println("Content:");
				//System.out.println("Class: " + fromServer.getContentHandler().toString());
				System.out.println("\n--------------------------------------------");
				//
				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}		
			this.ccli.showList();
			int inp =  -1;
			inp = this.ccli.get_input();
			if (-1 != inp)
			{
				Message toServer = createResponse(fromServer,inp);
				if(null != toServer)
					dst = toServer.marshall();
				try {
					if(cs.isConnected() == false)
					{
						System.out.println("[before write]socket disconnected");
						return;
					}
					out.write(dst);
					out.flush();
					//out.close();
					
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				System.out.println("-------------Sent----------!!!!");
			}
		}	
	}
	public Message createResponse(Message msg, int commandId)
	{
		Message resp = new Message();
		resp.setClientId(msg.getClientId());
		resp.setSessionId(msg.getSessionId());
		switch(commandId)
		{
		case 120:
			System.out.println("Create Command : INTRO");
			resp.setClientId(clientId);
			resp.setCommand(CommandsEnum.INTRO);
			Version ver = new Version();
			ver.setMajor(1);
			ver.setSubVer(12);
			resp.setContentSz(8);
			resp.setContentHandler(ver);
			//clientId++;
			break;
		case 160:
			System.out.println("Create Command : READY");
			resp.setCommand(CommandsEnum.READY);
			PlayerType pt = new PlayerType();
			pt.setType(1);
			resp.setContentHandler(pt);
			resp.setContentSz(10);
			break;
		case 150:
			System.out.println("Create Command : CHAT_MSG");
			resp.setCommand(CommandsEnum.CHAT_MSG);
			ChatMsg cm = new ChatMsg();
			cm.setSenderId(1);
			String phraze = "Hello all";
			byte pht[]=phraze.getBytes();
			cm.setAll(pht);
			System.out.println("Phraze len:" + pht.length);
			resp.setContentSz(4 + pht.length);
			resp.setContentHandler(cm);
			break;
		case 400:
			System.out.println("Create Command : Throw Dies");
			resp.setCommand(CommandsEnum.THR_DIES);
			resp.setContentSz(4);
			break;
		case 420:
			System.out.println("Create Command : Move Done");
			resp.setCommand(CommandsEnum.MOVE_DONE);
			resp.setContentSz(4);
			break;
//		case 200:
//			resp.setCommand(CommandsEnum.STATUS);
//			StatusData ndata = new StatusData();
//			ndata.setMode(1);
//			//ndata.setSecondsWait(100);
//			resp.setContentHandler(ndata);
//			break;
		case 0:
			System.exit(1);
		default:
			break;
		}
		return resp;
	}
}
	

